package com.joker.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.gitee.guoguoleo.annotation.JokerField;
import io.gitee.guoguoleo.annotation.JokerId;
import io.gitee.guoguoleo.annotation.JokerIgnore;
import io.gitee.guoguoleo.annotation.JokerLogic;
import io.gitee.guoguoleo.enums.FieldFill;
import io.gitee.guoguoleo.enums.IdType;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;

@Setter
@Getter
public abstract class BaseEntity implements Serializable {

    @JokerId(value = "id", type = IdType.ASSIGN_ID)
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    protected Long id;

    @JokerLogic
    protected Integer delFlag;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JokerField(fill = FieldFill.INSERT)
    protected LocalDateTime createTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JokerField(fill = FieldFill.UPDATE)
    protected LocalDateTime updateTime;

    protected String remarks;

    @JsonIgnore
    @JokerIgnore
    protected boolean newRecord = false;

    public boolean isNewRecord() {
        return newRecord || this.getId() == null;
    }

}
